window.onload = function(){
    var http = Http();
    var fn = function(name){
        return document.querySelector(name)
    };
    //查询数据
    getData();

    //提交数据
    fn(".submit").onclick= function(){
        var _name = fn(".name").value,
            _message = fn(".message").value;
        var data = JSON.stringify({
            name:_name,
            message:_message
        })
        http.request({
            method:'post',
            url:'/data/add',
            data,
            success:function(res){
                //查询数据
                getData();
            }
        })
    }
    //获取数据
    function getData(){
        http.request({
            method:"get",
            url:"/get_list",
            success:function(res){
                var data = JSON.parse(res);
                if(data.code == '200'){
                    //处理数据
                    var str = "";
                    for(var i=0;i<data.result.length;i++){
                        str +=`
                        <li class="list-group-item">${data.result[i].name}
                                        <span>说：</span>${data.result[i].message}
                                    </li> `
                    };
                    fn(".messageList").innerHTML = str;
                }
                
            }
        })
        

    }
}